﻿var selectedRowId = { Id: '-1' };
var idsOfSelectedRows = [];
var gridData = '#grdData';

//============================================
$(document).ready(function () {

    $(".datepicker").datepicker({
        showOn: "button",
        buttonImage: dateIconURL,
        changeMonth: true,
        changeYear: true,
        buttonImageOnly: true,
        dateFormat: 'mm/dd/yy',
        constrainInput: true
    });

    SetupGrid(GetgridOptions(), GetColumnNames, GetColumnModel, selectedRowId, idsOfSelectedRows, FillData, GetDataPosted('#grdData'), null, null);

    $('#btnRefresh').click(function () {
        //alert(tinymce.get('Contents').getContent());
        ClearText();
    });

    $('#btnFind').click(function () {
        TriggerReloadDataGrid(gridData);
    });

    $('#btnDelete').click(function () {
        DeleteNewsAll();
    });

});


//----------func cleartext()----------------------------------
function ClearText() {
    $('#Condition_Subject').val("");
    $('#Condition_Author').val("");
    $('#Condition_Contents').val("");
    $('#Subject').val("");
    $('#Author').val("");
    $('#Contents').val("");
    $('#CreationDate').val("");
    $('#LabelID').val("0");
    
}
//------------------------

//========func GetgridOption====================================
function GetgridOptions() {
    var obj = new Object();

    obj.Id = "#grdData";
    obj.Url = GetAllLabelPath;
    obj.Pager = "#pager";
    obj.IsAutoWidthColumn = false;
    obj.IsMultiSelect = false;
    obj.ColumnKeyIndex = "CreationDate";
    obj.DefaultSortName = "CreationDate";
    obj.DefaultSortType = "date";
    obj.DefaultSortOrder = "desc";
    //ClearText();
    /*
    $('#Condition_Subject').val("");
    $('#Condition_Author').val("");
    $('#Condition_Contents').val("");
    $('#Subject').val("");
    $('#Author').val("");
    $('#Contents').val("");
    $('#CreationDate').val("");
    $('#LabelID').val("0");
    */
    return obj;
}
//--------------------------------------------

//============================================
function GetColumnNames() {
    return ['Mã Label', 'Chủ đề', 'Lời Chúa', 'Cầu nguyện', 'Ngày tạo'];
}
//--------------------------------------------

//============================================
function GetColumnModel() {
    return [
            { name: 'LabelID', index: 'LabelID', hidden: true },
            { name: 'Subject', index: 'Subject', width: 200, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'Author', index: 'Author', width: 200, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'Contents', index: 'Contents', width: 400, align: "left", sortable: true, searchoptions: { sopt: ['cn', 'eq']} },
            { name: 'CreationDate', index: 'CreationDate', width: 150, align: "left", sortable: true }
            ];
}
//--------------------------------------------
//============================================
function FillData() {

    $('#LabelID').val(GetCellValue('#grdData', selectedRowId.Id, 'LabelID'));
    $('#Subject').val(GetCellValue('#grdData', selectedRowId.Id, 'Subject'));
    $('#Author').val(GetCellValue('#grdData', selectedRowId.Id, 'Author'));
    $('#Contents').val(GetCellValue('#grdData', selectedRowId.Id, 'Contents'));
    $('#CreationDate').val(GetCellValue('#grdData', selectedRowId.Id, 'CreationDate'));    
    return;
}
//--------------------------------------------
//============================================
function GetDataPosted(list) {

    return {

        Condition_Subject: function () { return $('#Condition_Subject').val(); },
        Condition_Author: function () { return $('#Condition_Author').val(); },
        Condition_Contents: function () { return $('#Condition_Contents').val(); },

        sortName: function () { return $(list).jqGrid('getGridParam', 'sortname'); },
        sortOrder: function () { return $(list).jqGrid('getGridParam', 'sortorder'); },
        rowNumber: function () { return $(list).jqGrid('getGridParam', 'rowNum'); },
        rowId: function () {
            return (updateData == true ? selectedRowId.Id : -1);
        },
        page: function () { return $(list).jqGrid('getGridParam', 'page'); }
    };
}
//--------------------------------------------




function DeleteNewsAll(e) {

    if ($('#LabelID').val() == "" || $('#LabelID').val() == "0") {
        OpenAlertPopup("Lời Chúa", "Chưa chọn mục tin để xóa");
        return;
    }

    OpenConfirmPopup("Lời Chúa", "Bạn có muốn xóa không?", fnDeleteNewsAll);

}

//=======================================
function fnDeleteNewsAll() {

    var opts = new Object();
    opts.ActionUrl = DeleteLabelAllPath;
    opts.data = { LabelID: $('#LabelID').val() };
    CallFunctionWithAjaxPostWithContentType(opts, fnDeleteNewsAllSuccessful, null, fnDeleteNewsAllError);
    //ClearText();
    $('#Condition_Subject').val("");
    $('#Condition_Author').val("");
    $('#Condition_Contents').val("");
    $('#Subject').val("");
    $('#Author').val("");
    $('#Contents').val("");
    $('#CreationDate').val("");
    $('#LabelID').val("0");
    
}
//---------------------------------------

//=======================================
function fnDeleteNewsAllSuccessful(data) {
    $('#Condition_Contents').val("");
    TriggerReloadDataGrid(gridData);
    OpenAlertPopup("Lời Chúa", data);
}
//---------------------------------------

//=======================================
function fnDeleteNewsAllError(data) {
    $('#Condition_Contents').val("");
    TriggerReloadDataGrid(gridData);
    OpenAlertPopup("Lời Chúa", data);
}
//---------------------------------------
